<?php

namespace plugin\cms\app\controller\admin;

use core\controller\AdminBase;
use core\lib\upload\UploadImage;
use Exception;
use plugin\cms\app\bll\Category as BllCategory;
use plugin\cms\app\bll\Model as BllModel;
use support\View;

class Cate extends AdminBase
{


    public function index()
    {

        if (request()->get('type', '') == 'selcates') {
            $bllCategory = new BllCategory();
            $cates = $bllCategory->cascader(request()->nowsiteid);

            $cates       = $bllCategory->cascader(
                siteid: request()->nowsiteid,
                tojson: true
            );
            return $this->success(json_decode($cates, true));
        } else {
            return $this->view();
        }



        return $this->view();
    }



    public function add()
    {
        $bllCategory = new BllCategory();
        if (is_post()) {

            try {
                $data = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllCategory->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllModel = new  BllModel();
            $models = $bllModel->select(
                siteid: [0, request()->nowsiteid]
            );


            $cates     = $bllCategory->cascader(request()->nowsiteid);
            $catetemps = $bllCategory->getUsableCateTemp(request()->nowsiteid);
            $pagetemps = $bllCategory->getUsablePageTemp(request()->nowsiteid);

            View::assign('cates', $cates);
            View::assign('models', $models);

            View::assign('catetemps', $catetemps);
            View::assign('pagetemps', $pagetemps);
            return $this->view();
        }
    }

    public function edit()
    {

        $id = request()->get('id');
        $bllCategory = new BllCategory();
        if (is_post()) {

            //删除栏目图片
            if (request()->input('submit_type', '') == 'uploadimg') {
                $uploadImage = new UploadImage();
                $result = $uploadImage->upload();
                return $this->success($result);
            } else {
                try {
                    $data = request()->post();
                    $data['siteid'] = request()->nowsiteid;
                    $bllCategory->edit($id, request()->nowsiteid, $data);
                } catch (Exception $ex) {
                    return $this->error($ex->getMessage());
                }
                return $this->success();
            }
        } else {


            $bllModel = new  BllModel();
            $models = $bllModel->select(
                siteid: [0, request()->nowsiteid]
            );
            $cates    = $bllCategory->cascader(request()->nowsiteid);
            $cateinfo = $bllCategory->find($id, request()->nowsiteid);

            $catetemps = $bllCategory->getUsableCateTemp(request()->nowsiteid);
            $pagetemps = $bllCategory->getUsablePageTemp(request()->nowsiteid);

            View::assign('cates', $cates);
            View::assign('models', $models);
            View::assign('cateinfo', $cateinfo);
            View::assign('catetemps', $catetemps);
            View::assign('pagetemps', $pagetemps);

            return $this->view();
        }
    }

    public function sort()
    {
        try {
            $bllCategory = new BllCategory();
            $bllCategory->sort(
                id: request()->input('id'),
                sort: request()->input('sort'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    public function delete()
    {
        try {
            $bllCategory = new BllCategory();
            $bllCategory->delete(
                id: request()->input('id'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    /**
     * 设置是否可以互动
     *
     * @return mixed
     */
    public function setInteract()
    {
        try {
            $bllCategory = new BllCategory();

            $type = request()->input('type');
            switch ($type) {
                case 'setComment':
                    $bllCategory->setComment(
                        id: request()->input('id'),
                        comment: request()->input('value'),
                        siteid: request()->nowsiteid
                    );
                    break;
                case 'setPraise':
                    $bllCategory->setPraise(
                        id: request()->input('id'),
                        praise: request()->input('value'),
                        siteid: request()->nowsiteid
                    );
                    break;
                case 'setCollect':
                    $bllCategory->setCollect(
                        id: request()->input('id'),
                        collect: request()->input('value'),
                        siteid: request()->nowsiteid
                    );
                case 'setContribute':
                    $bllCategory->setContribute(
                        id: request()->input('id'),
                        contribute: request()->input('value'),
                        siteid: request()->nowsiteid
                    );
                    break;
            }
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    /**
     * 计算栏目下的内容数量
     *
     * @return mixed
     */
    public function computeContentCount()
    {
        try {
            $bllCategory = new BllCategory();
            $bllCategory->computeContentPendingCount(request()->nowsiteid);
            $bllCategory->computeContentPublishedCount(request()->nowsiteid);
            $bllCategory->computeContentRecycleCount(request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
